#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include "../class/game.h"

#define FILENAME "../data/record/0.rcd"

void play() {
    srand((unsigned)time(NULL));
    Game game(4, 8, 2);
    game.display();
    while(1) {
        while (!_kbhit()) {}
        int id = _getch();
        int rst;
        switch (id) {
            case 72: 
                rst = game.go(1);
                break;
            case 80:
                rst = game.go(2);
                break;
            case 75:
                rst = game.go(3);
                break;
            case 77:
                rst = game.go(4);
                break;
            case 27: // Esc
                printf("Exit.\n");
                game.saveRecord(FILENAME);
                return;
                break;
        }
        game.display();
        if (rst < 0) {
            game.saveRecord(FILENAME);
            return;
        }
    }
}

void record(const char *filename) {
    Game game(filename);
	game.display();
	while (1) {
		while (!_kbhit()) {}
        int hit_id = _getch();
        if (hit_id  == 13) { // Enter
            int rst = game.next();
            game.display();
            if (rst < 0)
                return;
        } else if (hit_id == 27) { // Esc
            printf("Exit.\n");
            break;
        }        
	}
}

int main(int argc, char *argv[]) {
    if (argc == 1)
        play();
    else
        record(argv[1]);
    system("pause");
    return 0;
}